import alt from '../../app/alt'
import MessageActions from '../actions/message'

let resolve = null;
class MessageStore {

	constructor() {
		this.visible = false;
		this.type = 'default';
		this.timeout = 2000;
		this.callback = null;
		this.message = '';
		this.resolve = null;
		this.bindActions(MessageActions);
	}

	onShow(paylaod) {
		this.visible = true;
		let msg;
		if(paylaod.length >= 2) {
			msg = paylaod[0];
		} else {
			msg = paylaod;
		}
		for(var k in msg) {
			this[k] = msg[k];
		}
		if(paylaod[1] && typeof paylaod[1] == "function") this.resolve = paylaod[1];
		else {
			this.resolve = null;
		}
		setTimeout(() => {
			this.onHide();
		}, this.timeout);
	}

	onHide() {
		this.visible = false;
		this.emitChange();
		if(this.resolve) this.resolve();
	}
}

export default alt.createStore(MessageStore, 'MessageStore')
